package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpShipAgencyFeeHistory;
import com.ship.boats.domain.vo.QueryHistoryForwardingChargesInfo;
import com.ship.boats.domain.vo.QueryHistoryForwardingInfo;
import com.ship.boats.mapper.SpShipAgencyFeeHistoryMapper;
import com.ship.boats.service.BoatsReceivingStationService;
import com.ship.boats.service.SpShipAgencyFeeHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SpShipAgencyFeeHistoryServiceImpl extends ServiceImpl<SpShipAgencyFeeHistoryMapper, SpShipAgencyFeeHistory> implements SpShipAgencyFeeHistoryService {
    private static final Logger logger = LoggerFactory.getLogger(SpShipAgencyFeeHistoryServiceImpl.class);

    @Autowired
    private BoatsReceivingStationService boatsReceivingStationService;

    @Override
    public QueryHistoryForwardingInfo queryHistoryForwardingCharges(Integer pageNum, Integer pageSize, String id) {
        QueryHistoryForwardingInfo queryHistoryForwardingInfo = new QueryHistoryForwardingInfo();
        PageHelper.startPage(pageNum,pageSize);
        List<QueryHistoryForwardingChargesInfo> historyForwardingChargesInfoList = baseMapper.queryHistoryForwardingCharges(id);
        PageInfo<QueryHistoryForwardingChargesInfo> pageInfo = new PageInfo<>(historyForwardingChargesInfoList);
        queryHistoryForwardingInfo.setPageInfo(pageInfo);
        List<QueryHistoryForwardingChargesInfo> list = baseMapper.queryHistoryForwardingCharges(id);
        if (CollectionUtil.isNotEmpty(list)){
            BigDecimal shipAgencyFeeTotal = new BigDecimal(0);
            list.forEach(item -> {
                shipAgencyFeeTotal.add(item.getShipAgencyFee());
            });
            BigDecimal shipMeanValue = shipAgencyFeeTotal.divide(new BigDecimal(list.size()), 4, BigDecimal.ROUND_HALF_UP);
            queryHistoryForwardingInfo.setShipMeanValue(shipMeanValue);
        }
        return queryHistoryForwardingInfo;
    }

    @Override
    public List<QueryHistoryForwardingChargesInfo> exportHistoryForwardingCharges(String id) {
        return baseMapper.queryHistoryForwardingCharges(id);
    }
}
